package com.kou.service.impl;

import com.kou.dao.GoodsDao;
import com.kou.dao.GoodsDaoImpl;
import com.kou.pojo.Goods;
import com.kou.pojo.PageBean;
import com.kou.service.GoodsService;
import com.kou.utils.StrUtils;

import java.util.List;

/**
 * @author SteinY
 * @create 2022-03-25 15:16
 * @description:
 */
public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao = new GoodsDaoImpl();
    @Override
    public PageBean<Goods> pageBean(String tid, String currentPage) {
        if (!StrUtils.empty(currentPage)){
            currentPage = "1";
        }
        int pageSize = 4;
        //查询页内数据
        List<Goods> goodslist = goodsDao.findGoodslist(tid,currentPage,pageSize);
        //查询总条数
        Long totalCount = goodsDao.findGoodsCount(tid);

        PageBean<Goods> pageBean = new PageBean<>(goodslist,Integer.parseInt(currentPage),pageSize,totalCount);
        return pageBean;
    }

    @Override
    public Goods findGoodsById(String id) {
        return goodsDao.findGoodsById(id);

    }

    @Override
    public PageBean<Goods> select(String input, String currentPage) {
        if (!StrUtils.empty(currentPage)){
            currentPage = "1";
        }
        int pageSize = 8;
        List<Goods> goodslist = goodsDao.select(input,currentPage,pageSize);

        Long totalCount = goodsDao.selectCount(input);

        PageBean<Goods> pageBean = new PageBean<Goods>(goodslist,Integer.parseInt(currentPage),pageSize,totalCount);

        return pageBean;

    }

    @Override
    public List<Goods> findAll() {
        return goodsDao.findAll();

    }

    @Override
    public void create(Goods goods) {
        goodsDao.create(goods);
    }

    @Override
    public void deleteGoodsById(String id) {
        goodsDao.delete(id);
    }

    @Override
    public List<Goods> searchGoods(String name, String price) {
        return goodsDao.search(name,price);
    }

    @Override
    public void update(Goods good) {
        goodsDao.update(good);
    }
}
